library OfficerHoldPosition initializer init requires LegacySystem

	private function HoldingPattern takes nothing returns nothing
		local unit u = udg_DeltaOperator[1337]
		call ShiftMoveSpeed(u,-175.0)
		call UnitAddAbility(u,'A079') //Camouflage
		call UnitAddAbility(u,'A08G') //Hold Position 25 armor
		call PolledWait(5.)
		if GetWidgetLife(u)>0.405 then
			call ShiftMoveSpeed(u,175.0)
			call UnitRemoveAbility(u,'A079')
			call UnitRemoveAbility(u,'A08G') //Hold Position 25 armor
		endif
		set u = null
	endfunction

	private function SelectPlayerMarines takes nothing returns boolean
		return GetPlayerController(GetOwningPlayer(GetFilterUnit()))==MAP_CONTROL_USER
	endfunction

	private function HoldPositionConditions takes nothing returns boolean
		return GetSpellAbilityId()=='A061'
	endfunction

	private function HoldPosition takes nothing returns nothing
		local unit u = GetTriggerUnit()
		local unit u2
		local location l = GetUnitLoc(u)
		local integer i = 1
		local integer id = GetConvertedPlayerId(GetOwningPlayer(u))
		local integer id2
		local integer squad = 0
		local group g = AMGetUnitsInRange(1500,l,Condition(function SelectPlayerMarines))
		loop
			set u2 = FirstOfGroup(g)
			exitwhen u2==null
			set id2 = GetConvertedPlayerId(GetOwningPlayer(u2))
			if udg_Experience[id2]<udg_Experience[id] and udg_Experience[id2]<50000 then
				set udg_DeltaOperator[1337] = u2
				call IssueImmediateOrder(u2,"holdposition")
				call HoldingPattern.execute()
				set squad = 1
			endif
			call GroupRemoveUnit(g,u2)
		endloop
		if squad>0 then
			call PingMinimapEx(GetUnitX(u),GetUnitY(u),2.5,0,0,255,false)
			set bj_meleeNearestMineDist = GetUnitX(u)
			set bj_lastTransmissionDuration = GetUnitY(u)
			set udg_soundpath = "war3mapImported\\position.wav"
			call PlaySoundPath3D.execute()
		endif
		call DestroyGroup(g)
		set g = null
		call RemoveLocation(l)
		set l = null
		set u = null
		set u2 = null
	endfunction

	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_SPELL_EFFECT  )
		call TriggerAddCondition( t, Condition( function HoldPositionConditions ) )
		call TriggerAddAction(t,function HoldPosition)
	endfunction




endlibrary